<?php

namespace App\Http\Controllers\Frontend;

use App\Logics\NewsLogic;

class Components
{

    // 初始化
    public function __construct(array $data)
    {
        $this->data   = $data;
        $this->myUid  = $data['my_uid'] ?? 0;
        $this->module = $data['module'] ?? '';
    }

    // 栏目
    public function cate($cateId = 0,$is_count=null)
    {
        $option = $this->optLogic();
        $option->setField('cate_id', $cateId);
        $option->setField('rows', 15);
        $option->setField('is_count', $is_count);  // 不进行统计
        $option->setField('is_first', 1);  // 返回详情第一段
        $option->setField('title', null);  // 避免页面传值的时候进行搜索

        $request = request();
        $logic   = new NewsLogic();
        $result  = $logic->onLists($this->myUid);
        $max_id  = empty($result['list']) ? 0 : max(array_column($result['list'], 'id'));
        $min_id  = empty($result['list']) ? 0 : min(array_column($result['list'], 'id'));
        $m_time  = empty($result['list']) ? 0 : max(array_column($result['list'], 'ctime'));

        // 结果整理
        $result['nList']      = $this->makeNewsLists($result['list']);
        $result['max_id']     = $max_id;
        $result['min_id']     = $min_id;
        $result['max_time']   = $m_time;
        $result['cate_id']    = $cateId;
        $result['style_host'] = env('STYLE_HOST');
        $result['app_host']   = $request->getSchemeAndHttpHost().'/';
        $result['ip']         = $request->ip();
        // 组合页面
        $result['listHtml'] = $option->dispaly('frontend.component.cate', $result, false);


        return $result;
    }

    // 标签
    public function tag($tagId = 0)
    {
        $option = $this->optLogic();
        $option->setField('tag_id', $tagId);
        $option->setField('rows', 15);
        $option->setField('is_count', null);
        $option->setField('is_first', 1);  // 返回详情第一段
        $option->setField('title', null); // 避免页面传值的时候进行搜索


        $logic  = new NewsLogic();
        $result = $logic->onLists($this->myUid);
        // 结果整理
        $result['nList']      = $this->makeNewsLists($result['list']);
        $result['style_host'] = env('STYLE_HOST');
        $result['app_host']   = request()->getSchemeAndHttpHost().'/';


        return $option->dispaly('frontend.component.tag', $result, false);
    }


    // 搜索
    public function search($keyword)
    {
        $option = $this->optLogic();
        $option->setField('keyword', $keyword);
        $option->setField('rows', 15);
        $option->setField('is_count', null);
        $option->setField('is_first', 1);  // 返回详情第一段
        $option->setField('title', null); // 避免页面传值的时候进行搜索

        $logic  = new NewsLogic();
        $result = $logic->onLists($this->myUid);


        return $option->dispaly('frontend.component.search', $result, false);
    }


    ##################################################################################
    // 实例化 逻辑option
    protected function optLogic()
    {
        if(empty($this->optLogic)){
            $this->optLogic = (new \App\Service\Option())->setApp(request());
        }

        return $this->optLogic;
    }

    // 按日期整理
    protected function makeNewsLists($lists)
    {
        $nList = [];
        foreach($lists as $_row){
            $date = date('Y.m.d', $_row['ctime']);
            // 整理
            $_row['date'] = $date;
            // 整理
            $nList[$date][] = $_row;
        }

        return $nList;
    }

}
